/**
 * Created by sun on 2016/8/4.
 */
define(function(require) {
	var app = require('/../js/app');
	require('ui-table');



	require('upload');

	app.useModule("ui.table");


	app.useModule("ngFileUpload");
	var toastr =require('toastr');

	var datepicker = require('datepicker');
	app.controller('listCtrl', ['$scope', '$http','$filter','Upload',
		function($scope, $http,$filter,Upload) {
			$scope.add = {};
			$scope.list = {};
			$scope.imgs = [];
			$scope.orderParams = {
				url: "/admin/picture/list"
			};
			datepicker($scope);

			$scope.select2Options = {
				minimumResultsForSearch: Infinity //不出现搜索框
			};
			$scope.openMaxPic= function (url) {
				$scope.MaxPicModal = !$scope.MaxPicModal;
				$scope.url=url
			};

			//创建
			$scope.addOrderModal = function() {
				$scope.addModal = !$scope.addModal;
				$scope.add = {};
				$scope.pics=  {};
				$scope.imgs=[];
				$scope.ww='';
				$scope.hh='';
			};

			//全局查询
			$scope.search = function() {
				//表格查询参数设置
				$scope.orderParams.ajaxParam = $scope.ctrlScope.itemsearch;
				//表格刷新
				$scope.orderParams.reload();
			};

			//全局查询重置
			$scope.reset = function() {
				$scope.ctrlScope.itemsearch={};
				$scope.orderParams.ajaxParam={};
				$scope.orderParams.reload();
			};
			//新建保存
			$scope.adoptAdd = function(isValid) {
				if (!isValid) {
					toastr.error("信息填写有误");
				}
				var parm = $scope.add;
				parm.picturePath=$scope.pics;
				console.info(parm);
				$http({
					method: 'POST',
					url: "/admin/picture/add",
					data: parm
				}).success(function(data) {
					if(data.result== "success"){
						toastr.success("新建成功");
						$scope.addModal = !$scope.addModal;
						$scope.orderParams.reload();
					}else{
						toastr.error("新建失败");
					}

				}).error(function(data) {});
			};
			//删除
			$scope.delModal = function(id) {
				if (confirm("确认删除?")) {
					$http({
						method: 'POST',
						url: "/admin/picture/deleteById",
						data: {
							id: id
						},
					}).success(function(data) {
						console.log(data)
						if(data.result=='fail'){
							toastr.error("删除失败");
						}else{
							toastr.success("删除成功");
							$scope.orderParams.reload();
						}


					}).error(function(data) {});
				} else {
					return;
				}

			};

			//编辑
			$scope.modifyModal = function(id) {
				$scope.detailsModal = !$scope.detailsModal;
				$scope.getOne(id);
			};
			//单个查询
			$scope.getOne = function(id) {
				$http({
					method: 'POST',
					url: "/admin/picture/getSingle",
					data: {
						id: id
					}
				}).success(function(data) {
					console.info(data);
					$scope.list = data;
					$scope.imgs=[];
					$scope.imgs.push(data.picturePath);
					var tempImg=new Image();
					tempImg.src=data.picturePath;
					tempImg.onload = function() {
						//****************得到图片宽度
						var ww=tempImg.width;
						//****************得到图片高度
						var hh=tempImg.height;
						$scope.ww=ww+'px';
						$scope.hh=hh+'px'
					};
				}).error(function(data) {});
			};
			//保存
			$scope.saveSubmit = function() {
				var parm = $scope.list;
				parm.picturePath=$scope.pics;
				console.info(parm);
				$http({
					method: 'POST',
					url: "/admin/picture/edit",
					data: parm,
				}).success(function(data) {
					if(data.result== "success"){
						toastr.success("保存成功");
						$scope.detailsModal = !$scope.detailsModal;
						$scope.orderParams.reload();
					}else{
						toastr.error("保存失败");
					}


				}).error(function(data) {});
			};

			//上传图片
			$scope.selectUpload=function(){
				$scope.uploadTab.active=true;
			};

			$scope.onFileSelect = function (file) {
				console.log(file);


			};

			$scope.removeImg=function(index) {
				console.log(index);
				var imgs = $scope.imgs;
				imgs.splice(index,1);

				$scope.pics="";
				for(var i=0;i<imgs.length;i++){
					$scope.pics=$scope.pics+"|"+imgs[i];
				}
			};

			$scope.uploadPic =function(file) {
				console.log(Upload.resize);

				file.upload = Upload.upload({
					url: '/itemFile',
					headers: {
						'optional-header': 'header-value'
					},
					data: {username: $scope.username, file: file}
				});
				file.upload.then(function (response) {

					$scope.pics=response.data.url;


					//var vv=document.getElementsByName("file")[1].value;
					var tempImg=new Image();
					tempImg.src=$scope.pics;
					tempImg.onload = function() {
						//****************得到图片宽度
						  var ww=tempImg.width;
						//****************得到图片高度
						var hh=tempImg.height;
						$scope.ww=ww+'px';
						$scope.hh=hh+'px'
					};




					$scope.imgs.push(response.data.url);

					$scope.defaultPicFile="css/img/updatepic.png";
				}, function (response) {
					if (response.status > 0)
						$scope.errorMsg = response.status + ': ' + response.data;
				}, function (evt) {
					file.progress = Math.min(100, parseInt(100.0 * evt.loaded / evt.total));
					console.log(file.progress);
				});
				file.upload.xhr(function (xhr) {

				});
			};
		}
	]);
});